﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RelationTextBox
{
    public partial class RelationTextBoxControl : RelationTextBox, IDataGridViewEditingControl
    {
        public RelationTextBoxControl()
        {
            InitializeComponent();
            txBRelation.TextChanged += new EventHandler(txBRelation_TextChanged);
        }

        void txBRelation_TextChanged(object sender, EventArgs e)
        {
            NotifyDataGridViewOfValueChange();
        }
        /// <summary>
        /// Notifica gridul ca s-a modificat ceva in celula
        /// </summary>
        protected virtual void NotifyDataGridViewOfValueChange()
        {
            this.valueIsChanged = true;
            if (this.dataGridViewControl != null)
            {
                this.dataGridViewControl.NotifyCurrentCellDirty(true);
            }
        }

        /// <summary>
        /// Proprietate care indica daca controlul de editare trebuie repozitionat la schimbarea 
        /// valorii. Obligatoriu pentru interfata IDataGridViewEditingControl
        /// </summary>
        public bool RepositionEditingControlOnValueChange
        {
            get
            { 
                return false; 
            }
        }
        /// <summary>
        /// Proprietate care indica daca valoarea din controlul de editare s-a modificat sau nu.
        /// Obligatoriu pentru interfata IDataGridViewEditingControl
        /// </summary>
        /// 
        private Boolean valueIsChanged = false;
        public bool EditingControlValueChanged
        {
            get
            { 
                return valueIsChanged; 
            }
            set
            { 
                valueIsChanged = value; 
            }
        }
        /// <summary>
        /// Proprietaea care determina ce cursor tebuie folosit pentru panelul de editare,
        /// i.e. cursorul parintelui controlului de editare. Obligatoriu pentru interfata IDataGridViewEditingControl
        /// </summary>
        public Cursor EditingPanelCursor
        {
            get
            { 
                return base.Cursor; 
            }
        }
        /// <summary>
        /// Proprietate care reprezinta randul in care se afla controlul de editare
        /// Obligatoriu pentru interfata IDataGridViewEditingControl
        /// </summary>
        /// 
        private int rowIndexNum = 0;
        public int EditingControlRowIndex
        {
            get
            { 
                return rowIndexNum; 
            }
            set
            { 
                rowIndexNum = value; 
            }
        }
        /// <summary>
        /// Proprietate care reprezinta valoarea curenta de formatare a controlului de editare
        /// Valoare formatata reprezinta valoarea care va fi afisata in interfata utilizator!
        /// The formatted value represents the value as it is displayed in the control's user interface. 
        /// The formatted value may be different in absolute value and even data type from the actual value contained in the control.
        /// </summary>
        public object EditingControlFormattedValue
        {
            get
            {
                return this.txBRelation.Text;
            }
            set
            {
                this.txBRelation.Text = value.ToString();
            }
        }
        /// <summary>
        /// Proprietate pentru a accesa gridul care foloseste controlul
        /// </summary>
        private DataGridView dataGridViewControl=null;
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return (DataGridView)dataGridViewControl;
            }
            set
            {
                dataGridViewControl = (DataGridView)value;
            }
        }
        /// <summary>
        /// pregateste celula curent selectata pentru editare
        /// </summary>
        /// <param name="selectAll">true pentru a selecta intreg continutul celulei, altfel false</param>
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            if (selectAll)
                this.txBRelation.SelectAll();
                
        }
        /// <summary>
        /// Returneaza valoarea formatata a celulei
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }
        /// <summary>
        /// Determines whether the specified key is a regular input key that the editing control should process 
        /// or a special key that the DataGridView should process.
        /// </summary>
        /// <param name="key">A bitwise combination of Keys values that represents the key that was pressed.</param>
        /// <param name="dataGridViewWantsInputKey">true to indicate that the DataGridView control can process the key; otherwise, false</param>
        /// <returns></returns>
        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:
                    return true;
                default:
                    //return false;
                    return true ;
            }

        }
        /// <summary>
        /// Metoda apelata de grid inainte de a se afisa controlul ca sa poata fi adaptat 
        /// la stilul de celula furnizat
        /// </summary>
        public virtual void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            if (dataGridViewCellStyle.BackColor.A < 255)
            {
                // The NumericUpDown control does not support transparent back colors
                Color opaqueBackColor = Color.FromArgb(255, dataGridViewCellStyle.BackColor);
                this.BackColor = opaqueBackColor;
                this.dataGridViewControl.EditingPanel.BackColor = opaqueBackColor;
            }
            else
            {
                this.BackColor = dataGridViewCellStyle.BackColor;
            }
            this.ForeColor = dataGridViewCellStyle.ForeColor;
            //this.TextAlign = DataGridViewRelationTextBoxCell.TranslateAlignment(dataGridViewCellStyle.Alignment);

        }
    }
}
